<?php
/**
 * 《我的客户》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../contact/Customer.class.php');

class CustomerMy extends Customer {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct();
	}
	
	function tab($controller, $request) {		
		$data = array(
			// '潜在客户' => array('cc'=>'customer.php?do=PotentialMy.find', 'js'=>''),
			'我的客户' => array('cc'=>'contact.php?do=CustomerMy.find', 'js'=>'1'),
			'我的报价' => array('cc'=>'customer.php?do=QuoteMy.find', 'js'=>'1'),
			'我的产品' => array('cc'=>'mold.php?do=MoldMy.find', 'js'=>'1'),
			'货运需求' => array('cc'=>'express.php?do=RequirementCustomer.find', 'js'=>''),
			
			'我的收款' => array('cc'=>'finance.php?do=InMy.find', 'js'=>'1'),
			'收款明细' => array('cc'=>'finance.php?do=InItemMy.find', 'js'=>''),
			'我的到帐' => array('cc'=>'finance.php?do=TransactionMy.find', 'js'=>'1'),
			
			'我的提成' => array('cc'=>'finance.php?do=CommissionMy.find', 'js'=>''),
			// '跟单信息' => array('cc'=>'customer.php?do=Info.find', 'js'=>''),
			'付款信息' => array('cc'=>'finance.php?do=PaymentCustomer.find', 'js'=>''),
			
			'来访登记' => array('cc'=>'customer.php?do=VisitMy.find', 'js'=>''),
			'统计分析' => array('cc'=>'contact.php?do=Customer.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'contact/customer/my/find');
		return true;
	}
	
	function _my() {		
		$emp = $_SESSION['id'];
		
		return sprintf("visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['contact_'] = $this->_num_contact($row['id']);
				$row['bank_'] = $this->_num_bank($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'contact/customer/my/list');
		return true;
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->assign_by_ref('country_option', $this->code_option(Constant::tbl_country));
	
		$controller->assign_by_ref('contact', $this->_linkman($id));
	
		$controller->display($request, 'contact/customer/my/edit');
		return true;
	}
	
	function editPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		clean($this->tbl . "_id_" . $id);
	
		$code = $request['code'];
	
		$name = addslashes($request['name']);
		$fullname = addslashes($request['fullname']);
		$cert_type = $request['cert_type'];
		$cert_no = trim($request['cert_no']);
	
		$continent = $request['continent'];
		$country = $request['country'];
	
		$address = addslashes($request['address']);
		$geo = strtolower($request['geo']);
		$url = strtolower($request['url']);
	
		$jj = addslashes($request['jj']);
	
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET cert_type = '%s', cert_no = '%s', name = '%s', fullname = '%s', continent = '%s', country = '%s', address = '%s'";
		$fmt .= ", geo = '%s', url = '%s', jj = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $cert_type, $cert_no, $name, $fullname, $continent, $country, $address, $geo, $url, $jj, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		$contact = stripslashes($request['contact']);
		$this->_contact_id($id, $contact);
	
		$z = stripslashes($request['z']);
		$this->_delete($z);
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'contact/customer/my/upload');
		return true;
	}
}
?>